{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[DEBUG@graphviz.backend.execute] run [PosixPath('dot'), '-V']\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "('0.20.dev0', (2, 40, 1))"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%cd -q ..\n",
    "\n",
    "import logging\n",
    "\n",
    "import graphviz\n",
    "\n",
    "logging.basicConfig(format='[%(levelname)s@%(name)s] %(message)s', level=logging.DEBUG)\n",
    "\n",
    "graphviz.__version__, graphviz.version()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[DEBUG@graphviz.backend.execute] run [PosixPath('dot'), '-Kdot', '-Tsvg']\n"
     ]
    },
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.40.1 (20161225.0304)\n",
       " -->\n",
       "<!-- Title: %3 Pages: 1 -->\n",
       "<svg width=\"488pt\" height=\"116pt\"\n",
       " viewBox=\"0.00 0.00 488.38 116.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 112)\">\n",
       "<title>%3</title>\n",
       "<polygon fill=\"#ffffff\" stroke=\"transparent\" points=\"-4,4 -4,-112 484.3795,-112 484.3795,4 -4,4\"/>\n",
       "<!-- A -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>A</title>\n",
       "<ellipse fill=\"none\" stroke=\"#000000\" cx=\"239.0398\" cy=\"-90\" rx=\"66.0889\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"239.0398\" y=\"-86.3\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">King Arthur</text>\n",
       "</g>\n",
       "<!-- B -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>B</title>\n",
       "<ellipse fill=\"none\" stroke=\"#000000\" cx=\"115.0398\" cy=\"-18\" rx=\"115.0796\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"115.0398\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">Sir Bedevere the Wise</text>\n",
       "</g>\n",
       "<!-- A&#45;&gt;B -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>A&#45;&gt;B</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M210.9023,-73.6621C194.0767,-63.8924 172.4517,-51.3359 154.0004,-40.6223\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"155.6378,-37.5258 145.2324,-35.5312 152.1228,-43.5793 155.6378,-37.5258\"/>\n",
       "</g>\n",
       "<!-- L -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>L</title>\n",
       "<ellipse fill=\"none\" stroke=\"#000000\" cx=\"364.0398\" cy=\"-18\" rx=\"116.1796\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"364.0398\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">Sir Lancelot the Brave</text>\n",
       "</g>\n",
       "<!-- A&#45;&gt;L -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>A&#45;&gt;L</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M267.4042,-73.6621C284.3654,-63.8924 306.1649,-51.3359 324.765,-40.6223\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"326.6853,-43.5553 333.6037,-35.5312 323.1914,-37.4896 326.6853,-43.5553\"/>\n",
       "</g>\n",
       "<!-- B&#45;&gt;L -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>B&#45;&gt;L</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M230.2996,-18C232.6341,-18 234.9687,-18 237.3033,-18\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"237.5395,-21.5001 247.5395,-18 237.5395,-14.5001 237.5395,-21.5001\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7fe7ac3fc910>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dot = graphviz.Digraph(comment='The Round Table')\n",
    "\n",
    "dot.node('A', 'King Arthur')\n",
    "dot.node('B', 'Sir Bedevere the Wise')\n",
    "dot.node('L', 'Sir Lancelot the Brave')\n",
    "\n",
    "dot.edges(['AB', 'AL'])\n",
    "dot.edge('B', 'L', constraint='false')\n",
    "\n",
    "dot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[DEBUG@graphviz.backend.execute] run [PosixPath('dot'), '-Kdot', '-Tsvg']\n"
     ]
    },
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.40.1 (20161225.0304)\n",
       " -->\n",
       "<!-- Title: the holy hand grenade Pages: 1 -->\n",
       "<svg width=\"332pt\" height=\"44pt\"\n",
       " viewBox=\"0.00 0.00 332.00 44.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 40)\">\n",
       "<title>the holy hand grenade</title>\n",
       "<polygon fill=\"#ffffff\" stroke=\"transparent\" points=\"-4,4 -4,-40 328,-40 328,4 -4,4\"/>\n",
       "<!-- 1 -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>1</title>\n",
       "<ellipse fill=\"none\" stroke=\"#000000\" cx=\"27\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">1</text>\n",
       "</g>\n",
       "<!-- 2 -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>2</title>\n",
       "<ellipse fill=\"none\" stroke=\"#000000\" cx=\"117\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"117\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">2</text>\n",
       "</g>\n",
       "<!-- 1&#45;&gt;2 -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>1&#45;&gt;2</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M54.003,-18C62.0277,-18 70.9665,-18 79.5309,-18\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"79.7051,-21.5001 89.705,-18 79.705,-14.5001 79.7051,-21.5001\"/>\n",
       "</g>\n",
       "<!-- 3 -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>3</title>\n",
       "<ellipse fill=\"none\" stroke=\"#000000\" cx=\"207\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"207\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">3</text>\n",
       "</g>\n",
       "<!-- 2&#45;&gt;3 -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>2&#45;&gt;3</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M144.003,-18C152.0277,-18 160.9665,-18 169.5309,-18\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"169.7051,-21.5001 179.705,-18 169.705,-14.5001 169.7051,-21.5001\"/>\n",
       "</g>\n",
       "<!-- lob -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>lob</title>\n",
       "<ellipse fill=\"none\" stroke=\"#000000\" cx=\"297\" cy=\"-18\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"297\" y=\"-14.3\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">lob</text>\n",
       "</g>\n",
       "<!-- 3&#45;&gt;lob -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>3&#45;&gt;lob</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M234.003,-18C242.0277,-18 250.9665,-18 259.5309,-18\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"259.7051,-21.5001 269.705,-18 259.705,-14.5001 259.7051,-21.5001\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.sources.Source at 0x7fe7ac3fc390>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "src = graphviz.Source('digraph \"the holy hand grenade\" { rankdir=LR; 1 -> 2 -> 3 -> lob }')\n",
    "src"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[DEBUG@graphviz.backend.execute] run [PosixPath('dot'), '-Kdot', '-Tsvg']\n"
     ]
    },
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.40.1 (20161225.0304)\n",
       " -->\n",
       "<!-- Title: finite_state_machine Pages: 1 -->\n",
       "<svg width=\"576pt\" height=\"248pt\"\n",
       " viewBox=\"0.00 0.00 576.00 248.40\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(.7099 .7099) rotate(0) translate(4 345.8939)\">\n",
       "<title>finite_state_machine</title>\n",
       "<polygon fill=\"#ffffff\" stroke=\"transparent\" points=\"-4,4 -4,-345.8939 807.3633,-345.8939 807.3633,4 -4,4\"/>\n",
       "<!-- LR_0 -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>LR_0</title>\n",
       "<ellipse fill=\"none\" stroke=\"#000000\" cx=\"38.4469\" cy=\"-88.4469\" rx=\"34.3995\" ry=\"34.3995\"/>\n",
       "<ellipse fill=\"none\" stroke=\"#000000\" cx=\"38.4469\" cy=\"-88.4469\" rx=\"38.394\" ry=\"38.394\"/>\n",
       "<text text-anchor=\"middle\" x=\"38.4469\" y=\"-84.7469\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">LR_0</text>\n",
       "</g>\n",
       "<!-- LR_2 -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>LR_2</title>\n",
       "<ellipse fill=\"none\" stroke=\"#000000\" cx=\"188.3408\" cy=\"-159.4469\" rx=\"34.394\" ry=\"34.394\"/>\n",
       "<text text-anchor=\"middle\" x=\"188.3408\" y=\"-155.7469\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">LR_2</text>\n",
       "</g>\n",
       "<!-- LR_0&#45;&gt;LR_2 -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>LR_0&#45;&gt;LR_2</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M73.2124,-104.9142C95.4839,-115.4635 124.4765,-129.1964 147.795,-140.2417\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"146.5421,-143.5209 157.0778,-144.6386 149.5387,-137.1947 146.5421,-143.5209\"/>\n",
       "<text text-anchor=\"middle\" x=\"115.3939\" y=\"-138.2469\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">SS(B)</text>\n",
       "</g>\n",
       "<!-- LR_1 -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>LR_1</title>\n",
       "<ellipse fill=\"none\" stroke=\"#000000\" cx=\"188.3408\" cy=\"-45.4469\" rx=\"34.394\" ry=\"34.394\"/>\n",
       "<text text-anchor=\"middle\" x=\"188.3408\" y=\"-41.7469\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">LR_1</text>\n",
       "</g>\n",
       "<!-- LR_0&#45;&gt;LR_1 -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>LR_0&#45;&gt;LR_1</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M75.4994,-77.8177C96.6252,-71.7574 123.2023,-64.1332 145.2564,-57.8066\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"146.3881,-61.1232 155.0353,-55.0013 144.4578,-54.3945 146.3881,-61.1232\"/>\n",
       "<text text-anchor=\"middle\" x=\"115.3939\" y=\"-76.2469\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">SS(S)</text>\n",
       "</g>\n",
       "<!-- LR_3 -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>LR_3</title>\n",
       "<ellipse fill=\"none\" stroke=\"#000000\" cx=\"353.2347\" cy=\"-38.4469\" rx=\"34.3995\" ry=\"34.3995\"/>\n",
       "<ellipse fill=\"none\" stroke=\"#000000\" cx=\"353.2347\" cy=\"-38.4469\" rx=\"38.394\" ry=\"38.394\"/>\n",
       "<text text-anchor=\"middle\" x=\"353.2347\" y=\"-34.7469\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">LR_3</text>\n",
       "</g>\n",
       "<!-- LR_4 -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>LR_4</title>\n",
       "<ellipse fill=\"none\" stroke=\"#000000\" cx=\"353.2347\" cy=\"-303.4469\" rx=\"34.3995\" ry=\"34.3995\"/>\n",
       "<ellipse fill=\"none\" stroke=\"#000000\" cx=\"353.2347\" cy=\"-303.4469\" rx=\"38.394\" ry=\"38.394\"/>\n",
       "<text text-anchor=\"middle\" x=\"353.2347\" y=\"-299.7469\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">LR_4</text>\n",
       "</g>\n",
       "<!-- LR_8 -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>LR_8</title>\n",
       "<ellipse fill=\"none\" stroke=\"#000000\" cx=\"764.9164\" cy=\"-159.4469\" rx=\"34.3995\" ry=\"34.3995\"/>\n",
       "<ellipse fill=\"none\" stroke=\"#000000\" cx=\"764.9164\" cy=\"-159.4469\" rx=\"38.394\" ry=\"38.394\"/>\n",
       "<text text-anchor=\"middle\" x=\"764.9164\" y=\"-155.7469\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">LR_8</text>\n",
       "</g>\n",
       "<!-- LR_6 -->\n",
       "<g id=\"node7\" class=\"node\">\n",
       "<title>LR_6</title>\n",
       "<ellipse fill=\"none\" stroke=\"#000000\" cx=\"353.2347\" cy=\"-179.4469\" rx=\"34.394\" ry=\"34.394\"/>\n",
       "<text text-anchor=\"middle\" x=\"353.2347\" y=\"-175.7469\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">LR_6</text>\n",
       "</g>\n",
       "<!-- LR_8&#45;&gt;LR_6 -->\n",
       "<g id=\"edge13\" class=\"edge\">\n",
       "<title>LR_8&#45;&gt;LR_6</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M726.9091,-165.4889C679.8084,-172.6439 597.0195,-184.1344 525.5755,-188.4469 495.0117,-190.2918 487.2723,-189.7751 456.6817,-188.4469 437.3205,-187.6063 415.9784,-185.8847 397.7331,-184.1702\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"397.9588,-180.6757 387.6682,-183.1962 397.2846,-187.6432 397.9588,-180.6757\"/>\n",
       "<text text-anchor=\"middle\" x=\"558.5755\" y=\"-190.2469\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">S(b)</text>\n",
       "</g>\n",
       "<!-- LR_5 -->\n",
       "<g id=\"node8\" class=\"node\">\n",
       "<title>LR_5</title>\n",
       "<ellipse fill=\"none\" stroke=\"#000000\" cx=\"491.1286\" cy=\"-112.4469\" rx=\"34.394\" ry=\"34.394\"/>\n",
       "<text text-anchor=\"middle\" x=\"491.1286\" y=\"-108.7469\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">LR_5</text>\n",
       "</g>\n",
       "<!-- LR_8&#45;&gt;LR_5 -->\n",
       "<g id=\"edge14\" class=\"edge\">\n",
       "<title>LR_8&#45;&gt;LR_5</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M726.6271,-153.9547C691.452,-148.7973 637.9082,-140.6643 591.5755,-132.4469 572.9886,-129.1504 552.543,-125.1485 534.9769,-121.5931\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"535.5004,-118.128 525.0029,-119.5601 534.1023,-124.9869 535.5004,-118.128\"/>\n",
       "<text text-anchor=\"middle\" x=\"626.0225\" y=\"-147.2469\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">S(a)</text>\n",
       "</g>\n",
       "<!-- LR_2&#45;&gt;LR_4 -->\n",
       "<g id=\"edge6\" class=\"edge\">\n",
       "<title>LR_2&#45;&gt;LR_4</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M214.4688,-182.2642C242.0117,-206.3171 285.6174,-244.3975 316.5904,-271.4459\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"314.3408,-274.1281 324.1752,-278.0696 318.9453,-268.8556 314.3408,-274.1281\"/>\n",
       "<text text-anchor=\"middle\" x=\"268.7878\" y=\"-256.2469\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">S(A)</text>\n",
       "</g>\n",
       "<!-- LR_2&#45;&gt;LR_6 -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>LR_2&#45;&gt;LR_6</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M222.908,-163.6396C247.9462,-166.6765 282.0002,-170.8069 309.0187,-174.084\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"308.6472,-177.5645 318.9959,-175.2941 309.4901,-170.6154 308.6472,-177.5645\"/>\n",
       "<text text-anchor=\"middle\" x=\"268.7878\" y=\"-175.2469\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">SS(b)</text>\n",
       "</g>\n",
       "<!-- LR_2&#45;&gt;LR_5 -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>LR_2&#45;&gt;LR_5</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M220.8332,-147.3168C246.0707,-138.4771 282.1505,-127.0986 314.7878,-121.4469 359.0488,-113.7825 410.4168,-111.9436 446.2537,-111.7723\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"446.3926,-115.2723 456.3905,-111.7663 446.3885,-108.2723 446.3926,-115.2723\"/>\n",
       "<text text-anchor=\"middle\" x=\"353.2347\" y=\"-125.2469\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">SS(a)</text>\n",
       "</g>\n",
       "<!-- LR_1&#45;&gt;LR_3 -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>LR_1&#45;&gt;LR_3</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M222.908,-43.9795C246.5853,-42.9744 278.325,-41.627 304.5534,-40.5135\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"304.8328,-44.0049 314.6753,-40.0839 304.5358,-37.0112 304.8328,-44.0049\"/>\n",
       "<text text-anchor=\"middle\" x=\"268.7878\" y=\"-47.2469\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">S($end)</text>\n",
       "</g>\n",
       "<!-- LR_6&#45;&gt;LR_6 -->\n",
       "<g id=\"edge9\" class=\"edge\">\n",
       "<title>LR_6&#45;&gt;LR_6</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M340.1169,-211.4545C339.907,-222.7382 344.2796,-231.8939 353.2347,-231.8939 359.2514,-231.8939 363.1995,-227.7609 365.079,-221.6263\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"368.583,-221.8119 366.3525,-211.4545 361.6372,-220.9422 368.583,-221.8119\"/>\n",
       "<text text-anchor=\"middle\" x=\"353.2347\" y=\"-235.6939\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">S(b)</text>\n",
       "</g>\n",
       "<!-- LR_6&#45;&gt;LR_5 -->\n",
       "<g id=\"edge10\" class=\"edge\">\n",
       "<title>LR_6&#45;&gt;LR_5</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M384.525,-164.2436C404.1831,-154.6921 429.7327,-142.278 450.8909,-131.9977\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"452.4973,-135.1085 459.9622,-127.5901 449.4381,-128.8123 452.4973,-135.1085\"/>\n",
       "<text text-anchor=\"middle\" x=\"424.1817\" y=\"-155.2469\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">S(a)</text>\n",
       "</g>\n",
       "<!-- LR_5&#45;&gt;LR_5 -->\n",
       "<g id=\"edge8\" class=\"edge\">\n",
       "<title>LR_5&#45;&gt;LR_5</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M478.7566,-144.7125C478.6257,-155.8774 482.7497,-164.8939 491.1286,-164.8939 496.7582,-164.8939 500.467,-160.8237 502.2551,-154.7664\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"505.7446,-155.067 503.5006,-144.7125 498.7977,-154.2064 505.7446,-155.067\"/>\n",
       "<text text-anchor=\"middle\" x=\"491.1286\" y=\"-168.6939\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">S(a)</text>\n",
       "</g>\n",
       "<!-- LR_7 -->\n",
       "<g id=\"node9\" class=\"node\">\n",
       "<title>LR_7</title>\n",
       "<ellipse fill=\"none\" stroke=\"#000000\" cx=\"626.0225\" cy=\"-89.4469\" rx=\"34.394\" ry=\"34.394\"/>\n",
       "<text text-anchor=\"middle\" x=\"626.0225\" y=\"-85.7469\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">LR_7</text>\n",
       "</g>\n",
       "<!-- LR_5&#45;&gt;LR_7 -->\n",
       "<g id=\"edge7\" class=\"edge\">\n",
       "<title>LR_5&#45;&gt;LR_7</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M525.168,-106.6431C542.4218,-103.7012 563.5777,-100.0941 582.0497,-96.9445\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"582.7245,-100.38 591.994,-95.249 581.5479,-93.4796 582.7245,-100.38\"/>\n",
       "<text text-anchor=\"middle\" x=\"558.5755\" y=\"-107.2469\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">S(b)</text>\n",
       "</g>\n",
       "<!-- LR_7&#45;&gt;LR_8 -->\n",
       "<g id=\"edge11\" class=\"edge\">\n",
       "<title>LR_7&#45;&gt;LR_8</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M657,-104.732C672.4832,-112.4022 691.492,-121.8649 708.4694,-130.4469 712.7313,-132.6013 717.1681,-134.8574 721.5984,-137.1192\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"720.1286,-140.2987 730.6252,-141.7387 723.3177,-134.0672 720.1286,-140.2987\"/>\n",
       "<text text-anchor=\"middle\" x=\"693.4694\" y=\"-134.2469\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">S(b)</text>\n",
       "</g>\n",
       "<!-- LR_7&#45;&gt;LR_5 -->\n",
       "<g id=\"edge12\" class=\"edge\">\n",
       "<title>LR_7&#45;&gt;LR_5</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M592.6491,-80.1131C577.456,-77.3057 559.3781,-76.0067 543.5755,-80.4469 538.2486,-81.9437 532.927,-84.1479 527.8273,-86.7112\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"525.9854,-83.7304 518.9342,-91.6381 529.3777,-89.8535 525.9854,-83.7304\"/>\n",
       "<text text-anchor=\"middle\" x=\"558.5755\" y=\"-84.2469\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">S(a)</text>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7fe7ac39d890>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# http://www.graphviz.org/content/fsm\n",
    "\n",
    "f = graphviz.Digraph('finite_state_machine', filename='fsm.gv')\n",
    "\n",
    "f.attr(rankdir='LR', size='8,5')\n",
    "\n",
    "f.attr('node', shape='doublecircle')\n",
    "f.node('LR_0')\n",
    "f.node('LR_3')\n",
    "f.node('LR_4')\n",
    "f.node('LR_8')\n",
    "\n",
    "f.attr('node', shape='circle')\n",
    "f.edge('LR_0', 'LR_2', label='SS(B)')\n",
    "f.edge('LR_0', 'LR_1', label='SS(S)')\n",
    "f.edge('LR_1', 'LR_3', label='S($end)')\n",
    "f.edge('LR_2', 'LR_6', label='SS(b)')\n",
    "f.edge('LR_2', 'LR_5', label='SS(a)')\n",
    "f.edge('LR_2', 'LR_4', label='S(A)')\n",
    "f.edge('LR_5', 'LR_7', label='S(b)')\n",
    "f.edge('LR_5', 'LR_5', label='S(a)')\n",
    "f.edge('LR_6', 'LR_6', label='S(b)')\n",
    "f.edge('LR_6', 'LR_5', label='S(a)')\n",
    "f.edge('LR_7', 'LR_8', label='S(b)')\n",
    "f.edge('LR_7', 'LR_5', label='S(a)')\n",
    "f.edge('LR_8', 'LR_6', label='S(b)')\n",
    "f.edge('LR_8', 'LR_5', label='S(a)')\n",
    "\n",
    "f"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[DEBUG@graphviz.backend.execute] run [PosixPath('dot'), '-Kdot', '-Tsvg']\n"
     ]
    },
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
       "<!-- Generated by graphviz version 2.40.1 (20161225.0304)\n",
       " -->\n",
       "<!-- Title: G Pages: 1 -->\n",
       "<svg width=\"222pt\" height=\"364pt\"\n",
       " viewBox=\"0.00 0.00 222.00 364.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 360)\">\n",
       "<title>G</title>\n",
       "<polygon fill=\"#ffffff\" stroke=\"transparent\" points=\"-4,4 -4,-360 218,-360 218,4 -4,4\"/>\n",
       "<g id=\"clust1\" class=\"cluster\">\n",
       "<title>cluster0</title>\n",
       "<polygon fill=\"none\" stroke=\"#000000\" points=\"64,-152 64,-348 206,-348 206,-152 64,-152\"/>\n",
       "</g>\n",
       "<g id=\"clust2\" class=\"cluster\">\n",
       "<title>cluster1</title>\n",
       "<polygon fill=\"none\" stroke=\"#000000\" points=\"64,-8 64,-132 206,-132 206,-8 64,-8\"/>\n",
       "</g>\n",
       "<!-- a -->\n",
       "<g id=\"node1\" class=\"node\">\n",
       "<title>a</title>\n",
       "<ellipse fill=\"none\" stroke=\"#000000\" cx=\"135\" cy=\"-322\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"135\" y=\"-318.3\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">a</text>\n",
       "</g>\n",
       "<!-- b -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>b</title>\n",
       "<ellipse fill=\"none\" stroke=\"#000000\" cx=\"99\" cy=\"-250\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"99\" y=\"-246.3\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">b</text>\n",
       "</g>\n",
       "<!-- a&#45;&gt;b -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>a&#45;&gt;b</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M126.2854,-304.5708C122.0403,-296.0807 116.8464,-285.6929 112.1337,-276.2674\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"115.237,-274.6477 107.6343,-267.2687 108.976,-277.7782 115.237,-274.6477\"/>\n",
       "</g>\n",
       "<!-- c -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>c</title>\n",
       "<ellipse fill=\"none\" stroke=\"#000000\" cx=\"171\" cy=\"-250\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"171\" y=\"-246.3\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">c</text>\n",
       "</g>\n",
       "<!-- a&#45;&gt;c -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>a&#45;&gt;c</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M143.7146,-304.5708C147.9597,-296.0807 153.1536,-285.6929 157.8663,-276.2674\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"161.024,-277.7782 162.3657,-267.2687 154.763,-274.6477 161.024,-277.7782\"/>\n",
       "</g>\n",
       "<!-- d -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>d</title>\n",
       "<ellipse fill=\"none\" stroke=\"#000000\" cx=\"99\" cy=\"-178\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"99\" y=\"-174.3\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">d</text>\n",
       "</g>\n",
       "<!-- b&#45;&gt;d -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>b&#45;&gt;d</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M99,-231.8314C99,-224.131 99,-214.9743 99,-206.4166\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"102.5001,-206.4132 99,-196.4133 95.5001,-206.4133 102.5001,-206.4132\"/>\n",
       "</g>\n",
       "<!-- f -->\n",
       "<g id=\"node7\" class=\"node\">\n",
       "<title>f</title>\n",
       "<ellipse fill=\"none\" stroke=\"#000000\" cx=\"99\" cy=\"-34\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"99\" y=\"-30.3\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">f</text>\n",
       "</g>\n",
       "<!-- b&#45;&gt;f -->\n",
       "<g id=\"edge7\" class=\"edge\">\n",
       "<title>b&#45;&gt;f</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M113.1229,-234.2844C121.2535,-224.1468 130.6901,-210.2347 135,-196 141.9225,-173.1366 145.3998,-157.6593 145.432,-142.2164\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"148.9176,-141.8227 144.9572,-131.9959 141.9251,-142.1476 148.9176,-141.8227\"/>\n",
       "</g>\n",
       "<!-- c&#45;&gt;d -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>c&#45;&gt;d</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M155.7307,-234.7307C145.803,-224.803 132.6847,-211.6847 121.5637,-200.5637\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"123.7933,-197.8436 114.2473,-193.2473 118.8436,-202.7933 123.7933,-197.8436\"/>\n",
       "</g>\n",
       "<!-- e -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>e</title>\n",
       "<ellipse fill=\"none\" stroke=\"#000000\" cx=\"99\" cy=\"-106\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"99\" y=\"-102.3\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">e</text>\n",
       "</g>\n",
       "<!-- c&#45;&gt;e -->\n",
       "<g id=\"edge10\" class=\"edge\">\n",
       "<title>c&#45;&gt;e</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M135,-152C130.7681,-144.3996 125.3302,-136.7278 119.9697,-129.8859\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"122.3507,-127.2677 113.3176,-121.7311 116.9265,-131.6924 122.3507,-127.2677\"/>\n",
       "</g>\n",
       "<!-- g -->\n",
       "<g id=\"node6\" class=\"node\">\n",
       "<title>g</title>\n",
       "<ellipse fill=\"none\" stroke=\"#000000\" cx=\"171\" cy=\"-34\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"171\" y=\"-30.3\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">g</text>\n",
       "</g>\n",
       "<!-- c&#45;&gt;g -->\n",
       "<g id=\"edge9\" class=\"edge\">\n",
       "<title>c&#45;&gt;g</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M171,-152.004C171,-148.766 171,-145.516 171,-142.2656\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"174.5001,-141.9975 171,-131.9976 167.5001,-141.9976 174.5001,-141.9975\"/>\n",
       "</g>\n",
       "<!-- d&#45;&gt;e -->\n",
       "<g id=\"edge8\" class=\"edge\">\n",
       "<title>d&#45;&gt;e</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M99,-159.8314C99,-152.131 99,-142.9743 99,-134.4166\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"102.5001,-134.4132 99,-124.4133 95.5001,-134.4133 102.5001,-134.4132\"/>\n",
       "</g>\n",
       "<!-- h -->\n",
       "<g id=\"node8\" class=\"node\">\n",
       "<title>h</title>\n",
       "<ellipse fill=\"none\" stroke=\"#000000\" cx=\"27\" cy=\"-106\" rx=\"27\" ry=\"18\"/>\n",
       "<text text-anchor=\"middle\" x=\"27\" y=\"-102.3\" font-family=\"Times,serif\" font-size=\"14.00\" fill=\"#000000\">h</text>\n",
       "</g>\n",
       "<!-- d&#45;&gt;h -->\n",
       "<g id=\"edge11\" class=\"edge\">\n",
       "<title>d&#45;&gt;h</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M83.7307,-162.7307C73.803,-152.803 60.6847,-139.6847 49.5637,-128.5637\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"51.7933,-125.8436 42.2473,-121.2473 46.8436,-130.7933 51.7933,-125.8436\"/>\n",
       "</g>\n",
       "<!-- e&#45;&gt;g -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>e&#45;&gt;g</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M114.2693,-90.7307C124.197,-80.803 137.3153,-67.6847 148.4363,-56.5637\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"151.1564,-58.7933 155.7527,-49.2473 146.2067,-53.8436 151.1564,-58.7933\"/>\n",
       "</g>\n",
       "<!-- e&#45;&gt;f -->\n",
       "<g id=\"edge6\" class=\"edge\">\n",
       "<title>e&#45;&gt;f</title>\n",
       "<path fill=\"none\" stroke=\"#000000\" d=\"M99,-87.8314C99,-80.131 99,-70.9743 99,-62.4166\"/>\n",
       "<polygon fill=\"#000000\" stroke=\"#000000\" points=\"102.5001,-62.4132 99,-52.4133 95.5001,-62.4133 102.5001,-62.4132\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>\n"
      ],
      "text/plain": [
       "<graphviz.graphs.Digraph at 0x7fe7ac4083d0>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# http://www.graphviz.org/pdf/dotguide.pdf, Figure 20\n",
    "\n",
    "g = graphviz.Digraph('G', filename='cluster_edge.gv')\n",
    "\n",
    "g.attr(compound='true')\n",
    "\n",
    "with g.subgraph(name='cluster0') as c:\n",
    "    c.edges(['ab', 'ac', 'bd', 'cd'])\n",
    "\n",
    "with g.subgraph(name='cluster1') as c:\n",
    "    c.edges(['eg', 'ef'])\n",
    "\n",
    "g.edge('b', 'f', lhead='cluster1')\n",
    "g.edge('d', 'e')\n",
    "g.edge('c', 'g', ltail='cluster0', lhead='cluster1')\n",
    "g.edge('c', 'e', ltail='cluster0')\n",
    "g.edge('d', 'h')\n",
    "\n",
    "g"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
